package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;
    @Override
    public List<Role> findAll() {
        List<Role> roleList = roleDao.findAll();
        return roleList;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);

        setRoleAndPermission(role,permissionIds);

        setRoleAndMenu(role,menuIds);
    }

    @Override
    public void delete(String id) {
        //根据检查组id查询是否有关联的检查项
        List<Integer> permissions = roleDao.findPermissionIdsByRoleId(id);
        List<Integer> menus = roleDao.findMenuIdsByRoleId(id);
        if (permissions.size()>0||menus.size()>0){
            throw new RuntimeException("当前角色被引用，不能删除");
        }
        roleDao.delete(id);
    }

    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(String id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(String id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.edit(role);
        roleDao.deleteAssociationPermission(role.getId());
        roleDao.deleteAssociationMenu(role.getId());

        setRoleAndPermission(role,permissionIds);
        setRoleAndMenu(role,menuIds);
    }

    private void setRoleAndMenu(Role role, Integer[] menuIds) {
        Integer roleId = role.getId();

        if (menuIds!=null&&menuIds.length>0){
            for (Integer menuId : menuIds) {
                roleDao.setRoleAndMenu(roleId,menuId);
            }
        }
    }

    private void setRoleAndPermission(Role role, Integer[] permissionIds) {
        Integer roleId = role.getId();

        if (permissionIds!=null&&permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                roleDao.setRoleAndPermission(roleId,permissionId);
            }
        }
    }
}
